//********************************************************************************************************************//
//ORGANIZATION:
//AUTHOR: bpeng(bogers.peng@gmail.com)
//SUMMARY:
//********************************************************************************************************************//
#ifndef NATIVE_SYSTEM_BASE_ITERATORBUILDER_H
#define NATIVE_SYSTEM_BASE_ITERATORBUILDER_H

#ifndef NATIVE_SYSTEM_BASE_ARRAYITERATOR_H
#include "System/Base/ArrayIterator.h"
#endif
#ifndef NATIVE_SYSTEM_BASE_MAPITERATOR_H
#include "System/Base/MapIterator.h"
#endif

namespace Native { namespace System
{
    //****************************************************************************************************************//
    //UMapIteratorBuilder
    //
    //****************************************************************************************************************//
    template<typename _TElement>
    struct NATIVE_LIBRARY_EXPORT UArrayIteratorBuilder
    {
        NATIVE_REDEFINE(public, _TElement, TElement, T_ELEMENT);

        inline static CArrayIterator<TElement, true>
            CreateForward(const CArray<TElement>* array) throw()
        {
            try {
                return CArrayIterator<TElement, true>(array);
            } catch(CException& e) {
                CScene scene(
                    L"Arcane::System::UArrayIteratorBuilder<>",
                    L"CreateForward",
                    __LINE__);
                throw CCaughtException(scene, e);
            }
        }

        inline static CArrayIterator<TElement, false>
            CreateBackward(const CArray<TElement>* array) throw()
        {
            try {
                return CArrayIterator<TElement, false>(array);
            } catch(CException& e) {
                CScene scene(
                    L"Arcane::System::UArrayIteratorBuilder<>",
                    L"CreateBackward",
                    __LINE__);
                throw CCaughtException(scene, e);
            }
        }
    };

    //****************************************************************************************************************//
    //UMapIteratorBuilder
    //
    //****************************************************************************************************************//
    template<
        typename _TKey,
        typename _TValue,
        typename _FToString = function<wstring(const _TKey&)>
    >
    struct NATIVE_LIBRARY_EXPORT UMapIteratorBuilder
    {
        NATIVE_REDEFINE(public, _TKey, TKey, T_KEY);
        NATIVE_REDEFINE(public, _TValue, TValue, T_VALUE);
        typedef _FToString UToString;

        inline static CMapIterator<TKey, TValue, true>
            CreateForward(const CMap<TKey, TValue, UToString>* map)
        {
            try {
                return CMapIterator<TKey, TValue, true>(map, Have);
            } catch(CException& e) {
                CScene scene(
                    L"Arcane::System::UMapIteratorBuilder<>",
                    L"CreateForward",
                    __LINE__);
                throw CCaughtException(scene, e);
            }
            
        }

        inline static CMapIterator<TKey, TValue, false>
            CreateBackward(const CMap<TKey, TValue, UToString>* map)
        {
            try {
                return CMapIterator<TKey, TValue, false>(map);
            } catch(CException& e) {
                CScene scene(
                    L"Arcane::System::UMapIteratorBuilder<>",
                    L"CreateBackward",
                    __LINE__);

                throw CCaughtException(scene, e);
            }
        }
    };
}}

#endif